package com.gs.core.web.controllers;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.Principal;
import java.util.Iterator;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;

import com.gs.core.domain.User;
import com.gs.core.service.HelloWorldService;

@Controller
@RequestMapping("/hw/")
public class HelloWorldController {
	
	@Autowired
	HelloWorldService helloWorldService;
	
	@RequestMapping("/admin/list")
	public String show(@RequestParam("id") long id,Model model){
		//id is input parameter
		//model is output parameter
		//request gets dispatched to a jsp named helloWorld.jsp
		System.out.println("Got it : " + id+ " using @RequestParam");
		model.addAttribute("id", id+ " using @RequestParam");
		model.addAttribute("message", helloWorldService.getMessage());
		return "helloWorld";
	}
	@RequestMapping("/admin/list/{id}")
	public String showUsingPathVariable(@PathVariable("id") long id,Model model){
		//id is input parameter
		//model is output parameter
		//request gets dispatched to a jsp named helloWorld.jsp
		System.out.println("Got it : " + id + " using @PathVariable");
		model.addAttribute("id", id+ " using @PathVariable");
		return "helloWorld";
	}
	
	@RequestMapping("/admin/showParameters")
	public String showAllHandlerParameters(
				Locale locale,
				WebRequest webRequest,
				//InputStream is,
				//OutputStream os,
				Principal p,
				//@PathVariable Long id,
				@RequestParam Long id,
				@RequestHeader("Accept-Encoding") String encoding,
//				@RequestBody String body,
//				//HttpEntity<User> user,
//				Errors errors,
				SessionStatus status
	){
		System.out.println("WebRequest : " +webRequest);
		System.out.println("Locale : " + locale);
		System.out.println("Principal " + p);
		System.out.println("Encoding ? " + encoding);
		System.out.println("Session status: " + status);
		System.out.println("webRequest.getHeaderNames() " + webRequest.getHeaderNames());
		for (Iterator iterator = webRequest.getHeaderNames(); iterator.hasNext();) {
			String headerName = (String) iterator.next();
			System.out.println(headerName);
		}
		return "helloWorld";
	}
	
}
